<?php
// +----------------------------------------------------------------------
// | Author: wyd 2025/10/14
// | DESC:
// +----------------------------------------------------------------------

declare (strict_types=1);

require_once __DIR__."../../../vendor/autoload.php";

use Doctrine\Common\Annotations\AnnotationReader;


//1.1、定义
//定义一个注解类，需要另一个类去使用，才会有意义
/**
 * @Annotation
 * @Target({"CLASS","PROPERTY","METHOD"})
 */
class Db
{
    public $conn_url;
}

// 1.2、调用注解
/**
 * @Db(conn_url="mysql:host=localhost;dbname=test")   #需要用到定义注解类的属性
 */
class User {
    /**
     * @Db(conn_url="abc")      #参数必须 ，即括号中必须赋值
     */
    public $url;

    /**
     * @Db(conn_url="doing somethings")
     */
    public function do()
    {
    }
}


//下面的代码可以认为是在User类里的代码
$reader = new AnnotationReader();

//2 使用注解
//// 2.1、读取类注解
//$reflectionClass = new \ReflectionClass(User::class);
////getClassAnnotations只返回有业务意义的注解，过滤掉用于定义注解结构的元注解(@Annotation、@Target、@Attributes)
////方法1:
//$annotations = $reader->getClassAnnotation($reflectionClass,Db::class);
//var_dump($annotations);
////方法2:
//$classAnnotations = $reader->getClassAnnotations($reflectionClass,Db::class);
//foreach ($classAnnotations as $annotation) {
//    var_dump($annotation);
//    //echo get_class($annotation) . "\n";         //输出Db
//}

// 读取属性注解
//方法1
//$property = $reader->getPropertyAnnotation(new ReflectionProperty(User::class,'url'),Db::class);
//var_dump($property->conn_url);
////方法2
//$propertys = $reader->getPropertyAnnotations(new ReflectionProperty(User::class,'url'));
//foreach ($propertys as $property) {
//    var_dump($property->conn_url);          //输出abc
//}

// 读取方法注解
//方法1：
//$method = $reader->getMethodAnnotation(
//    new \ReflectionMethod(User::class, 'do'),Db::class
//);
//var_dump($method);
////方法2：
//$methods = $reader->getMethodAnnotations(
//    new \ReflectionMethod(User::class, 'do')
//);
//foreach ($methods as $method) {
//    var_dump($method->conn_url);          //输出abc
//}

/**********************************************************************************************************************/
//使用举例
/**
 * @Annotation
 * @Target("CLASS")
 */
class Entity {
    public $table;
}

/**
 * @Annotation
 * @Target("PROPERTY")
 */
class Column {
    public $type;
    public $length;
}

/**
 * @Entity(table="users")
 */
class Users {
    /**
     * @Column(type="string", length=255)
     */
    private $name;

    /**
     * @Column(type="integer")
     */
    private $age;
}

$reader = new AnnotationReader();

// 读取类注解
$reflectionClass = new ReflectionClass('Users');
$classAnnotations = $reader->getClassAnnotations($reflectionClass);

foreach ($classAnnotations as $annotation) {
    var_dump($annotation);
//    if ($annotation instanceof Entity) {
//        echo "Entity 表名: " . $annotation->table . "\n";
//    }
}

// 读取属性注解
$reflectionProperties = $reflectionClass->getProperties();
foreach ($reflectionProperties as $property) {
    $propertyAnnotations = $reader->getPropertyAnnotations($property);
    foreach ($propertyAnnotations as $annotation) {
        if ($annotation instanceof Column) {
            echo "属性 {$property->getName()} 的列类型: {$annotation->type}, 长度: {$annotation->length}\n";
        }
    }
}